import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class FileDataStorage {
    private static final String USER_FILE = "users.dat";
    private static final String MOVIE_FILE = "movies.dat";
    private static final String SCHEDULE_FILE = "schedules.dat";

    // 保存用户列表到文件
    public static void saveUsers(List<User> users) {
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(USER_FILE))) {
            oos.writeObject(users);
        } catch (IOException e) {
            System.out.println("保存用户数据失败: " + e.getMessage());
        }
    }

    // 从文件加载用户列表
    @SuppressWarnings("unchecked")
    public static List<User> loadUsers() {
        File file = new File(USER_FILE);
        if (!file.exists()) {
            // 如果文件不存在，返回默认用户列表
            System.out.println("[调试] 用户文件不存在，使用默认用户列表");
            return createDefaultUsers();
        }

        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(USER_FILE))) {
            List<User> users = (List<User>) ois.readObject();
            System.out.println("[调试] 从文件加载用户成功，用户数: " + users.size());
            return users;
        } catch (IOException | ClassNotFoundException e) {
            System.out.println("加载用户数据失败，使用默认数据: " + e.getMessage());
            return createDefaultUsers();
        }
    }

    // 保存影片列表到文件
    public static void saveMovies(List<Movie> movies) {
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(MOVIE_FILE))) {
            oos.writeObject(movies);
        } catch (IOException e) {
            System.out.println("保存影片数据失败: " + e.getMessage());
        }
    }

    // 从文件加载影片列表
    @SuppressWarnings("unchecked")
    public static List<Movie> loadMovies() {
        File file = new File(MOVIE_FILE);
        if (!file.exists()) {
            // 如果文件不存在，返回默认影片列表
            return createDefaultMovies();
        }

        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(MOVIE_FILE))) {
            return (List<Movie>) ois.readObject();
        } catch (IOException | ClassNotFoundException e) {
            System.out.println("加载影片数据失败，使用默认数据: " + e.getMessage());
            return createDefaultMovies();
        }
    }

    // 保存场次列表到文件
    public static void saveSchedules(List<Schedule> schedules) {
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(SCHEDULE_FILE))) {
            oos.writeObject(schedules);
        } catch (IOException e) {
            System.out.println("保存场次数据失败: " + e.getMessage());
        }
    }

    // 从文件加载场次列表
    @SuppressWarnings("unchecked")
    public static List<Schedule> loadSchedules() {
        File file = new File(SCHEDULE_FILE);
        if (!file.exists()) {
            // 如果文件不存在，返回默认场次列表
            return createDefaultSchedules();
        }

        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(SCHEDULE_FILE))) {
            return (List<Schedule>) ois.readObject();
        } catch (IOException | ClassNotFoundException e) {
            System.out.println("加载场次数据失败，使用默认数据: " + e.getMessage());
            return createDefaultSchedules();
        }
    }

    // 创建默认用户列表（确保admin密码正确）
    private static List<User> createDefaultUsers() {
        List<User> users = new ArrayList<>();
        // 强制设置admin密码为ynuinfo#777的MD5加密值
        String adminPwd = MD5Util.encrypt("ynuinfo#777");
        System.out.println("[调试] 创建默认用户，admin加密密码: " + adminPwd);

        users.add(new User("admin", adminPwd, "admin"));
        users.add(new User("manager", MD5Util.encrypt("manager123"), "manager"));
        users.add(new User("front", MD5Util.encrypt("front123"), "front"));
        return users;
    }

    // 创建默认影片列表
    private static List<Movie> createDefaultMovies() {
        List<Movie> movies = new ArrayList<>();
        movies.add(new Movie("M001", "流浪地球", "125min", "科幻", 50.0));
        movies.add(new Movie("M002", "满江红", "159min", "悬疑", 45.0));
        return movies;
    }

    // 创建默认场次列表
    private static List<Schedule> createDefaultSchedules() {
        List<Schedule> schedules = new ArrayList<>();
        schedules.add(new Schedule("S001", "M001", "黄金厅", "2023-10-01", "19:00", 50));
        schedules.add(new Schedule("S002", "M002", "白银厅", "2023-10-01", "20:30", 40));
        return schedules;
    }
}
